package com.study.zzedu.fragment.tab.catalog;


import android.content.Context;
import android.text.TextUtils;

import com.study.zzedu.base.BaseModel;
import com.study.zzedu.http.OnResultListener;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

public class CatalogModel extends BaseModel {
    /*
    通过课程编号查询课程目录
     */
    public void getCourseContent(Context context,String courseCode, final OnResultListener resultListener) {
        if (TextUtils.isEmpty(AppConstant.userCode)) {
            LogUtil.e("Forget", "学号异常");
            return;
        }
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/appCourseContent/getContentByCourseCode",
                new String[]{"courseCode"}, new String[]{courseCode}, new OkHttpUtils.HttpCallBack() {

            @Override
            public void onSuccess(String obj) {
                try {
                    JSONObject jObject = new JSONObject(obj);
                    if (0 == jObject.getInt("code")) {
                        LogUtil.e("Forget", "课程目录-通过课程编号查询课程目录成功");
                        resultListener.onSuccess(obj);
                    } else {
                        LogUtil.e("Forget", "课程目录-通过课程编号查询课程目录失败");
                        resultListener.onError(jObject.get("message").toString());
                    }
                } catch (Exception e) {
                    e.toString();
                }
            }

            @Override
            public void onSuccess(JSONObject obj) {
                resultListener.onSuccess(obj.toString());
            }

            @Override
            public void onError(String msg) {
                LogUtil.e("Forget", "课程目录-通过课程编号查询课程目录失败");
                resultListener.onError(msg);
            }
        });
    }

    public void getVideoURL(Context context,String id, final OnResultListener resultListener) {

        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/appCourseSubject/getCoursePlayUrl",
                new String[]{"id"}, new String[]{id}, new OkHttpUtils.HttpCallBack() {

                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                LogUtil.e("Forget", "查询播放路径成功");
                                resultListener.onSuccess(obj);
                            } else {
                                LogUtil.e("Forget", "查询播放路径失败");
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        LogUtil.e("Forget", "查询播放路径失败");
                        resultListener.onError(msg);
                    }
                });
    }
}
